const cloud = require('@cloudbase/node-sdk');

const app = cloud.init({
  env: cloud.SYMBOL_CURRENT_ENV
});

const db = app.database();
const _ = db.command;

exports.main = async (event, context) => {
  try {
    const { artistId } = event;
    
    if (!artistId) {
      throw new Error('artistId is required');
    }

    const artist = await db.collection('artists')
      .doc(artistId)
      .get();

    if (!artist.data.length) {
      throw new Error('Artist not found');
    }

    const artworks = await db.collection('artworks')
      .where({
        artistId: artistId
      })
      .limit(10)
      .get();

    return {
      code: 0,
      data: {
        profile: artist.data[0],
        recentWorks: artworks.data
      },
      message: 'success'
    };
  } catch (error) {
    return {
      code: 1,
      message: error.message
    };
  }
};